clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/***************/
/* Estimations */
/***************/
capture program drop regsamp
program define regsamp
version 11
syntax, occp(name) outc(name)

use final.dta, clear

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 

xi , pre(REG_) i.nreg

gen occ="bcq" if bcq==1
replace occ="cad" if cad==1

/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=(`occp'==0 & L.`occp'==1)
replace oshift=( (cad==0 & L.cad==1) | (tecemp2==0 & L.tecemp2==1) | (bcq==0 & L.bcq==1) | (bcnq==0 & L.bcnq==1)) if "`occp'"=="all"

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse r`outc' dimm dimm75 Y_* REG_* , by(ze an)

/* OLS */
ivreg2 r`outc' dimm Y_*  REG_* , partial(Y_* REG_*) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 r`outc' (dimm  = dimm75 ) Y_* REG_* , partial(Y_* REG_*) cl(ze)
estimates store `occp'iv

use temp1.dta, clear
ivreg2 r`outc' (dimm inter = dimm75 inter75) lqinit  Y_*  REG_* [aweight = iw2], partial(Y_* REG_*) cl(ze)
estimates store `occp'ivs

test dimm + inter = 0
end

capture program drop mobtyp
program define mobtyp
version 11
syntax, outc(name) file(name) 

/* OUTFLOWS */
regsamp, occp(all) outc(`outc')
regsamp, occp(cad) outc(`outc')
regsamp, occp(tecemp2) outc(`outc')
regsamp, occp(bcq) outc(`outc')
regsamp, occp(bcnq) outc(`outc')
/*
regsamp, occp(trad) outc(`outc')
regsamp, occp(ntrad) outc(`outc')
regsamp, occp(constbc) outc(`outc')
*/

capture rm "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\\`file'.rtf"
/* OLS */
esttab allols  cadols tecemp2ols bcqols bcnqols /* bcols tradols ntradols constbcols */ ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cadiv tecemp2iv bcqiv bcnqiv /* bciv tradiv ntradiv constbciv */ ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IVSELECT */
esttab allivs  cadivs tecemp2ivs bcqivs bcnqivs /* bcivs tradivs ntradivs constbcivs */ ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

end

mobtyp , outc(lshift) file(outflows)
mobtyp , outc(oshift) file(occup)
mobtyp , outc(dzbascom) file(bascom)
mobtyp , outc(dzbasman) file(basman)

/* Effet moyen pour occupations première colonne */
capture program drop regsampa
program define regsampa
version 11
syntax, occp(name) outc(name)

use final.dta, clear

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94"

xi , pre(REG_) i.nreg
 
/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=( (cad==0 & L.cad==1) | (tecemp2==0 & L.tecemp2==1) | (bcq==0 & L.bcq==1) | (bcnq==0 & L.bcnq==1))

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse r`outc' dimm dimm75 Y_* REG_* , by(ze an)

/* OLS */
ivreg2 r`outc' dimm Y_*  REG_* , partial(Y_* REG_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 r`outc' (dimm  = dimm75 ) Y_* REG_* , partial(Y_* REG_* ) cl(ze)
estimates store `occp'iv

use temp1.dta, clear
ivreg2 r`outc' (dimm inter = dimm75 inter75) lqinit  Y_* REG_*  [aweight = iw2], partial(Y_* REG_* ) cl(ze)
estimates store `occp'ivs

end
regsampa , occp(all) outc(oshift)

estout allols alliv allivs, cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 widstat) starlevels(* 0.10 ** 0.05 *** 0.01)

/***********************/
/* Employment balanced */
/***********************/
capture program drop empl
program define empl
version 11
syntax, occp(name) outc(name) sample(name)

/********************/
/* Balanced Sample  */
/********************/
use  `sample'.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen ldp=ln(`outc')
/* residual wages for each year */
quietly: reg ldp F_* 
predict rldp, residuals
/* change in residual wages */
gen drldp=D.rldp


gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drldp */
quietly: sum drldp if an==`year', d
replace drldp=. if drldp>`r(p99)' & an==`year'
replace drldp=. if drldp<`r(p1)' & an==`year'
}
drop if missing(drldp)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drldp dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drldp dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drldp (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

end

empl, occp(all) outc(dp) sample(final)
empl, occp(cad) outc(dp) sample(final)
empl, occp(tecemp2) outc(dp) sample(final)
empl, occp(bcq) outc(dp) sample(final)
empl, occp(bcnq) outc(dp) sample(final)

capture rm "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\regresult\emplBl.rtf"
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbcqiv bsbcnqiv ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\emplBl.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

empl, occp(all) outc(dpP) sample(final_3y)
empl, occp(cad) outc(dpP) sample(final_3y)
empl, occp(tecemp2) outc(dpP) sample(final_3y)
empl, occp(bcq) outc(dpP) sample(final_3y)
empl, occp(bcnq) outc(dpP) sample(final_3y)

capture rm "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\regresult\emplBl3y.rtf"
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbcqiv bsbcnqiv ///
using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\regresult\emplBl3y.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/*******************************************************/
/* EMPLOYMENT BALANCED OCCUPATION AND LOCATION STAYERS */
/*******************************************************/
capture program drop emplst
program define emplst
version 11
syntax, occp(name) outc(name)

use final.dta, clear
/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')


/* location in the decile of the initial group distribution */
gen ldp=ln(`outc')
/* residual wages for each year */
quietly: reg ldp F_* 
predict rldp, residuals
/* change in residual wages */
gen drldp=D.rldp

gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drldp if an==`year', d
replace drldp=. if drldp>`r(p99)' & an==`year'
replace drldp=. if drldp<`r(p1)' & an==`year'
}
drop if missing(drldp)
drop if missing(basman)
drop if missing(bascom)

drop if lshift==1
drop if oshift==1

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drldp dimm dimm75 Y_*, by(ze an)

/* IV */
ivreg2 drldp (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store s`occp'iv

end

emplst, occp(all) outc(dp)
emplst, occp(cad) outc(dp)
emplst, occp(tecemp2) outc(dp)
emplst, occp(bcq) outc(dp)
emplst, occp(bcnq) outc(dp)

capture rm "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\emplSt.rtf"
/* IV Balanced */
esttab salliv  scadiv stecemp2iv sbcqiv sbcnqiv ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\emplSt.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/*********************/
/* WAGES WAGES WAGES */
/*********************/
capture program drop wages
program define wages
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

use temp1.dta, clear
gen shinter=dimm*lshift
gen shinter75=dimm75*lshift
gen ointer=dimm*oshift
gen ointer75=dimm75*oshift

ivreg2 drlsnd (dimm  = dimm75 ) Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c1

ivreg2 drlsnd (dimm shinter ointer = dimm75 shinter75 ointer75) Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c2

ivreg2 drlsnd (dimm shinter  = dimm75 shinter75 ) lshift Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c2bis

ivreg2 drlsnd (dimm  ointer = dimm75 ointer75) oshift Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c3bis

ivreg2 drlsnd (dimm shinter ointer = dimm75 shinter75 ointer75) lshift oshift Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c3

ivreg2 drlsnd (dimm = dimm75 ) Y_* if lshift==1 [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c4

capture ivreg2 drlsnd (dimm  = dimm75 ) Y_* if oshift==1 [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c5

estout c1 c2 c2bis c3bis c3 c4 c5, cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 rkf) starlevels(* 0.10 ** 0.05 *** 0.01)

esttab c1 c2 c2bis c3bis c3 c4 c5 ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\Interwages.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/***********/
/* STAYERS */
/***********/
use final.dta, clear
/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

drop if lshift==1
drop if oshift==1

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store s`occp'iv

end

capture rm "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\Interwages.rtf"

wages, occp(all) 
wages, occp(cad) 
wages, occp(tecemp2) 
wages, occp(bc) 
wages, occp(bcnq) 

capture rm "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\wages.rtf"
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\wages.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
 
 /* IV Stayers */
esttab salliv scadiv stecemp2iv sbciv sbcnqiv ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\wages.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/*****************************/
/* WAGES WAGES WAGES 3 YEARS */
/*****************************/
capture program drop wages3y
program define wages3y
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final_3y.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(snPT/dpP)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

end

wages3y, occp(all) 
wages3y, occp(cad) 
wages3y, occp(tecemp2) 
wages3y, occp(bc) 
wages3y, occp(bcnq) 

capture rm "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\wages3y_new.rtf"
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\wages3y_new.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/***************************/
/* TABLE DIRECTION OF MOVE */
/***************************/
capture program drop regrapid3
program define regrapid3
version 11
syntax, occp(name) 

/* weight inverse of the size of the group in the CZ */
use final.dta, clear

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 
gen lnbze=ln(zepop)

xi , pre(REG_) i.nreg

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)
keep if lshift==1
/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
gen adimm2=adimm-dimm

/* change immig */
ivreg2 dzeimmig dimm   Y_* REG_* [aweight = iw2] , partial( Y_* REG_*) cl(ze)
estimates store mv0

/* change immig growth */
ivreg2 adimm2 (dimm = dimm75)  Y_*  REG_* [aweight = iw2] , partial( Y_* REG_*) cl(ze)
estimates store mv1

/* housing costs */
ivreg2 liloy (dimm = dimtm2)  if an==2007 [aweight = iw2] ,  cl(ze)
estimates store mv4

esttab mv0  mv1  mv4 ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\dirmov.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

use temp.dta, clear
end

capture rm "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\dirmov.rtf"
regrapid3, occp(all)
regrapid3, occp(cad)
regrapid3, occp(tecemp2)
regrapid3, occp(bcq)
regrapid3, occp(bcnq)

/***************************************/
/* QELOCUP 							   */
/* OUTCOME : CADRE TECHNICIAN EMPLOYEE */
/***************************************/
capture program drop quelocup
program define quelocup
version 11
syntax, occp(name) 

use final.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* CORRIGER CORRIGER residual outcomes */
quietly: reg cad F_*  if btime==2
predict rcad if btime==2, residuals
quietly: reg tecemp2 F_*  if btime==2
predict rtecemp2 if btime==2, residuals
quietly: reg bcq F_*  if btime==2
predict rbcq if btime==2, residuals
quietly: reg bcnq F_*  if btime==2
predict rbcnq if btime==2, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
ivreg2 rcad (dimm inter = dimm75 inter75) lqinit  Y_* [aweight = iw], partial(Y_* ) cl(ze)
estimates store cad
test dimm + inter = 0

ivreg2 rtecemp2 (dimm inter = dimm75 inter75) lqinit  Y_* [aweight = iw], partial(Y_* ) cl(ze)
estimates store tecemp2
test dimm + inter = 0

ivreg2 rbcq (dimm inter = dimm75 inter75) lqinit  Y_* [aweight = iw], partial(Y_* ) cl(ze)
estimates store rbcq
test dimm + inter = 0

ivreg2 rbcnq (dimm inter = dimm75 inter75) lqinit  Y_* [aweight = iw], partial(Y_* ) cl(ze)
estimates store rbcnq
test dimm + inter = 0

esttab cad tecemp2 rbcq rbcnq ///
using "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\quelocup.rtf" /// 
 , append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

sum cad tecemp2 bcq bcnq
end

use final.dta, clear
capture rm "C:\Users\AJUSTSA_G_VERDUGO\Documents\AJUSTSA_G_VERDUGO\Documents\ImmPanelRevis17\Donnees\regresult\quelocup.rtf"
quelocup, occp(bcq)
quelocup, occp(bcnq)

/******************/
/* Baseline rates */
/******************/
use final.dta, clear


capture program drop bastab
program define bastab
version 11
syntax, occp(name)

use final.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
gen dbascom=D.bascom
gen dbasman=D.basman
gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace

end

capture program drop bastab2
program define bastab2
version 11
syntax, outc(name)

/* OUTFLOWS */
capture quietly bastab, occp(all) 
display "all `outc'"
sum `outc'
quietly bastab, occp(cad) 
display "cad `outc'"
sum `outc'
quietly bastab, occp(tecemp2) 
display "tecemp2 `outc'"
sum `outc'
quietly bastab, occp(bcq) 
display "bcq `outc'"
sum `outc'
quietly bastab, occp(bcnq) 
display "bcnq `outc'"
sum `outc'

end
bastab2 , outc(lshift) 
bastab2 , outc(oshift) 

